package com.translate.web.service.impl;

import com.translate.web.dao.NoticeCenterMapper;
import com.translate.web.param.contract.ISearchNoticeParam;
import com.translate.web.param.contract.IUserIdParam;
import com.translate.entity.NoticeCenter;
import com.translate.web.service.INoticeCenterService;
import com.basic.service.impl.BaseServiceImpl;
import com.common.util.CopyEntity;
import com.param.contract.IPage;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Service;


@Service
public class NoticeCenterServiceImpl extends BaseServiceImpl<NoticeCenter> implements INoticeCenterService {

	public NoticeCenterServiceImpl(NoticeCenterMapper mapper) {
		super(mapper);
	}

	protected void insertPropteris(NoticeCenter data) {
		long time = System.currentTimeMillis() / 1000;
		
		data.setCreateTime(time);
		
		data.setReadStatus((byte) 0);
		
		data.setUpdateTime(time);
	}

	protected void updatePropteris(NoticeCenter data) {
		data.setUpdateTime(System.currentTimeMillis() / 1000);
	}

	protected Class<NoticeCenter> getEntityClass() {
		return NoticeCenter.class;
	}
	
	@Override
	protected <S extends IPage> String sqlByGetCounttAfterProcesser(S param) {
		
		ISearchNoticeParam userIdParam = (ISearchNoticeParam) param;
		
		String ids = getTypes(param);
		
		return super.sqlByGetCount(" user_id = "+ userIdParam.getUserId() + " and platform=0 and notice_type in("
				+ ids+")"
				);
	}
	
	private <S extends IPage> String getTypes(S param) {
		
		ISearchNoticeParam userIdParam = (ISearchNoticeParam) param;
		
		int[] type = userIdParam.getType();
		
		StringBuilder sb = new StringBuilder();
		
		for (int i = 0; i < type.length; i++) {
			sb.append(type[i]).append(",");
		}
		
		return sb.substring(0, sb.length()-1);
		
	}

	@Override
	protected <S extends IPage> String sqlByGetPageListAfterProcesser(S param) {
		
		ISearchNoticeParam userIdParam = (ISearchNoticeParam) param;
		
		String ids = getTypes(param);

		return super.sqlByGetPageList(" and user_id = "+ userIdParam.getUserId() + " and platform=0 and notice_type in("+ids + ")"+" order by create_time desc");
	}

	@Override
	public int countNotRead(int userId) {
		
		NoticeCenterMapper mapper = this.getBaseMapper();
		
		return mapper.countNotRead(userId);
	}

	@Override
	public <P, VO> List<VO> noticeCentersByUserId(P param, Class<VO> target) {
		List<NoticeCenter> datasByIdAndUserId = this.mapper.getDatasByIdAndUserId(param);

		List<VO> vos = new ArrayList<VO>();
		
		for (NoticeCenter noticeCenter : datasByIdAndUserId) {
			vos.add(CopyEntity.copyEntity(noticeCenter, target));
		}
		
		return vos;
	}

	@Override
	public int countRewardUser(int userId) {
		
		NoticeCenterMapper mapper = this.getBaseMapper();
		
		return mapper.countRewardUser(userId);
	}

	@Override
	public int insertNoticeCenter(int noticeType, int userId, int typeId, String content) {
		
		NoticeCenter noticeCenter = new NoticeCenter();
		
		noticeCenter.setNoticeType((byte) noticeType);
		
		noticeCenter.setUserId(userId);
		
		noticeCenter.setTypeId(typeId);
		
		noticeCenter.setTitle("创建订单");
		
		noticeCenter.setNoticeContent(content);
		
		noticeCenter.setPlatform((byte) 0);
		
		return this.insert(noticeCenter);
	}
	

	@Override
	public int orderNotice(String title, int userId, int typeId, String content, Byte platform) {
		
		NoticeCenter noticeCenter = new NoticeCenter();
		
		noticeCenter.setNoticeType((byte) 0);
		
		noticeCenter.setUserId(userId);
		
		noticeCenter.setTypeId(typeId);
		
		noticeCenter.setAdminId(0);
		
		noticeCenter.setTitle(title);
		
		noticeCenter.setNoticeContent(content);
		
		noticeCenter.setReadStatus((byte) 0);
		noticeCenter.setPlatform(platform);
		
		return this.insert(noticeCenter);
	}

	@Override
	public Integer[] countOrderAndSecondaryServiceNotice(int userId) {
	
		NoticeCenterMapper noticeCenterMapper = this.getBaseMapper();
		
		return noticeCenterMapper.countOrderAndSecondaryServiceNotice(userId);
	}

	


}